package com.fbinggun.fbinggunutil.command;


import com.fbinggun.fbinggunutil.FPlugin;
import com.fbinggun.fbinggunutil.API.JavaUtil;
import com.fbinggun.fbinggunutil.command.args.reload;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 命令主类 创建类后自动注册指令 （需先在plugin.yml文件中注册）
 */
public abstract class MainCommand implements CommandExecutor , TabExecutor {

    public  Map<String,ACommand> argsCommand = new HashMap<>();
    FPlugin plugin;
    String cmd;
    public MainCommand(FPlugin plugin,String cmd) {
        this.plugin=plugin;
        this.cmd=cmd;
        plugin.getCommand(cmd).setExecutor(this);
        plugin.getCommand(cmd).setTabCompleter(this);
        new reload(this);
    }

    public String getCmd() {
        return cmd;
    }

    /**
     * 主指令介绍
     * @return
     */
    public abstract String getPresentation();

    public FPlugin getPlugin(){
       return plugin;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String s, String[] args) {
        if(args==null||args.length==0){
            sender.sendMessage(getPlugin().getLang(JavaUtil.replaceColor(getPresentation()),false));
            for(ACommand aCommand:argsCommand.values()){
                if(aCommand.isOp()){
                    if(sender.isOp()) {
                        sender.sendMessage(getPlugin().getLang(JavaUtil.replaceColor(aCommand.getPresentation()), false));
                    }
                }else {
                    sender.sendMessage(getPlugin().getLang(JavaUtil.replaceColor(aCommand.getPresentation()), false));
                }
            }
            return false;
        }
        ACommand ac = argsCommand.get(args[0].toLowerCase());
        if(ac!=null){
           return ac.runCommand(sender,args);
        }
        return false;
    }



    @Override
    public  List<String> onTabComplete(CommandSender commandSender, Command command,  String s,  String[] strings) {
        List<String> list = new ArrayList<>();
        int l =0;
        if(strings!=null||strings.length==0){
            l=strings.length;
        }
        if(l==1) {

            for (ACommand ac : argsCommand.values()) {
                if(ac.isOp()){
                    if(commandSender.isOp()){
                       list.add(ac.getId());
                    }
                }else {
                    list.add(ac.getId());
                }

            }
            return list;
        }
        for (ACommand ac : argsCommand.values()) {
            if(strings[0].equalsIgnoreCase(ac.getId())) {
                if(ac.isOp()){
                    if(commandSender.isOp()){
                        list.addAll(ac.getTAB(l, commandSender,strings));
                    }
                }else {
                    list.addAll(ac.getTAB(l, commandSender,strings));
                }
            }
        }
        return list;
    }
}
